package com.shigu.user.action;

import com.shigu.MallResponse;
import com.shigu.address.bo.UserAddressBo;
import com.shigu.address.vo.AddressResponse;
import com.shigu.component.enums.SessionEnum;
import com.shigu.component.shiro.AuthorityUser;
import com.shigu.exceptions.WapException;
import com.shigu.trade.bo.*;
import com.shigu.trade.service.PageTradeService;
import com.shigu.trade.service.TradeService;
import com.shigu.trade.vo.ConfirmTradeResponse;
import com.shigu.trade.vo.PayByWxResponse;
import com.shigu.trade.vo.PostFeeResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/*
 * create by  过儿  2019-05-16
 *
 */
@Controller
@RequestMapping("wapsjson")
public class ShareTradeController {
    @Autowired
    PageTradeService pageTradeService;
    @Autowired
    TradeService tradeService;

    @RequestMapping("getH5TradePostFee")
    @ResponseBody
    public PostFeeResponse getH5TradePostFee( SharePostFeeRequest request){

        return pageTradeService.getH5TradePostFee(request.getProvId(),request.getTradeCode());
    }

    @RequestMapping("compeleteH5OrderInfo")
    @ResponseBody
    public MallResponse compeleteH5OrderInfo(HttpSession session, ShareAddrRequest request) throws Exception {
        AuthorityUser user = (AuthorityUser) session.getAttribute(SessionEnum.LOGIN_USER.toString());

        UserAddressBo bo = new UserAddressBo();
        bo.setProvId(request.getProvId());
        bo.setAreaId(request.getAreaId());
        bo.setCityId(request.getCityId());
        bo.setReceiveAddress(request.getReceiveAddress());
        bo.setReceiveName(request.getReceiveName());
        bo.setReceiveTelephone(request.getReceiveTelephone());
        bo.setImDefault(true);

        return pageTradeService.compeleteH5OrderInfo(bo,request.getTradeCode(),user.getUserId());
    }

    @RequestMapping("getAddressInfo")
    @ResponseBody
    public AddressResponse getAddressInfo(HttpSession session){
        AuthorityUser user = (AuthorityUser) session.getAttribute(SessionEnum.LOGIN_USER.toString());
        return pageTradeService.getAddressInfo(user.getUserId());
    }

    @RequestMapping("confirmTradeH5")
    @ResponseBody
    public ConfirmTradeResponse confirmTradeH5(ConfirmTradeRequest request,HttpSession session) throws WapException {
        AuthorityUser user = (AuthorityUser) session.getAttribute(SessionEnum.LOGIN_USER.toString());
        return tradeService.confirmTrade(request.getNum(), request.getItemId(), request.getSkuId(), user.getUserId(),request.getSharerToken());
    }


    @RequestMapping("getWxPayInfo")
    @ResponseBody
    public PayByWxResponse getWxPayInfo(HttpSession session, H5PayRequest request) throws Exception {
        AuthorityUser user = (AuthorityUser) session.getAttribute(SessionEnum.LOGIN_USER.toString());
        return pageTradeService.getWxPayInfo(request.getTradeCode(),user.getUserId());
    }
}
